import { _decorator, Component, Node, sys, resources, Asset, AudioSource, AudioClip, TextAsset, assetManager, tween, ProgressBar, Vec3, v3, Tween } from 'cc';
import GD from '../../config/GD';
import DataManager from '../../config/DataManager';
import { tyqSDK } from '../../tyq3.x/tyq-sdk';
import TyqEventMgr from '../../tyq3.x/tyq-event-mgr';
import SdkMgr from '../../tyq3.x/SdkMgr';
import { EType } from '../../tyq3.x/Platform/wechat/WXCustomAd';
import GDef from "../../config/GDef";
import Tool from "../../tool/Tool";
import { Notifications } from "../../easyFramework/notifications";
const { ccclass, property } = _decorator;

@ccclass('load')
export class load extends Component {
    @property(Node) preLayer: Node = null;
    private maxNum: number = 6;
    private loadNum: number = 0;
    private tweenProgressNum: number = 0;
    _continue: boolean = false;
    _loadFinished: boolean = false;
    @property(Node) title: Node = null;
    @property(Node) btn_continue: Node = null;
    @property(Node) nodeLoading: Node = null;
    @property(ProgressBar) comProgress: Node = null;
    @property(ProgressBar) comProgress2: Node = null;
    @property(AudioClip) sound: AudioClip = null;
    @property(AudioSource) audioSource: AudioSource = null;
    onLoad() {
        this.tweenProgressNum = 0;
        GD.init();
        resources.load("config", function (err, data) {
            if (err) {
                console.error(err);
                return;
            }
            jsonTables.initTableConfig(data.json);

        }.bind(this));

        this.loadLeveData();
        tyqSDK.init(() => {
            tyqSDK.login();
            TyqEventMgr.ins.sendEvent('进入加载页');
            Tool.showGridAD();
        });
        SdkMgr.ins.preLoadAd();
        // this.btn_continue.active = false;
        // this.scheduleOnce(() => {
        //     this.btn_continue.active = true;
        // }, 2)
        // tween(this.title).by(1,{ position:v3(0,20,0)},{easing:"quadOut"}).by(0.2,{ position:v3(0,-20,0)}).union().repeatForever().start();
        tween(this.btn_continue).delay(2).to(0.3, { eulerAngles: v3(0, 0, 10) }).delay(0.5).to(0.1, { eulerAngles: v3(0, 0, -10) }).to(0.1, { eulerAngles: v3(0, 0, 10) }).to(0.05, { eulerAngles: v3(0, 0, 0) }).union().repeatForever().start();
    }
    loadLeveData() {
        var self = this;
        // bundle.load('btn', AudioClip, function (err, sound) {
        //     if (err) {
        //         console.error(err);
        //         return;
        //     }
        //     AudioSourceComponent.playOneShot(audioClip);
        // });

        console.log("loadLeveData");
        if (sys.platform === sys.Platform.WECHAT_GAME) {
            this.wxLoadPack();
        } else {
            console.log("loadDir");
            // assetManager.loadBundle('leveData', function (err,bundle) {
            //     if (err) {
            //         console.log('leveData分包加载失败')
            //     }
            //     // console.log("leveData");
            //     bundle.loadDir('/',TextAsset,(completedCount: number, totalCount: number, item: any) => {
            //         // 表格数据处理
            //         // console.log(JSON.stringify(item));
            //         let items = item.file.text.split('\n');
            //         DataManager.handleData(item.file._name, items);
            //         // 进度条数据处理
            //     }, (completedCount: number, totalCount: number, item: any) => {
            //         // cc.director.loadScene("gameScene");
            //         self._loadFinished = true;
            //         self.enterGame();
            //     });
            // });
            assetManager.loadBundle("pack1", function (err, bundle) {
                if (err) {
                    console.log('pack1分包加载失败');
                }
                console.log('pack1分包加载成功');
                self.completed();
                self._loadFinished = true;
                self.enterGame();
            })
            // loader.loadResDir('leveData', (completedCount: number, totalCount: number, item: any) => {
            //     // 表格数据处理
            //     let items = item.content.text.split('\n');
            //     DataManager.handleData(item.content.name, items);
            //     // 进度条数据处理
            // }, () => {
            //     // cc.director.loadScene("gameScene");
            //     this._loadFinished = true;
            //     this.enterGame();
            // })
        }
    }

    wxLoadPack() {
        var that = this;
        console.log('wxLoadPack');
        this.tweenProgressNum = 1;
        assetManager.loadBundle("pack1", function (err, bundle) {
            if (err) {
                console.log('pack1分包加载失败')
            }
            console.log('pack1分包加载成功');
            that.completed();
            that._loadFinished = true;
            that.enterGame();
        })
    }

    private completed() {
        this.loadNum++;
        if (this.loadNum == this.maxNum) {

            this._loadFinished = true;
            this.enterGame();
        }
    }
    private enterGame() {
        if (!this._continue || !this._loadFinished) {
            return;
        }
        if (this.comProgress) {
            this.comProgress.progress = 1;
            this.tweenProgressNum = 1;
        }
        cc.director.loadScene("gameScene");
        Tool.hideGridAD();
    }
    onClickContinue() {
        // 注意：第二个参数 “volumeScale” 是指播放音量的倍数，最终播放的音量为 “audioSource.volume * volumeScale”
        this.audioSource.playOneShot(this.sound, 1);



        var self = this;
        this.nodeLoading.active = true;
        this.comProgress.node.active = true;
        this.comProgress2.node.active = true;
        const tweenTargetVec3 = new Vec3();
        self.comProgress2.progress = 0;
        this.tweenProgress2 = new Tween(tweenTargetVec3)
            .by(0.6, new Vec3(1, 1, 1), {
                'onUpdate': (target: object | undefined, ratio: number | undefined) => {
                    self.comProgress2.progress = ratio;
                }
            }).repeatForever();
        this.tweenProgress2.start();

        self.comProgress.progress = 0;
        const tweenTargetVec32 = new Vec3();
        this.tweenProgress = new Tween(tweenTargetVec32)
            .by(1, new Vec3(1, 1, 1), {
                'onUpdate': (target: object | undefined, ratio: number | undefined) => {
                    if (self.comProgress.progress < self.tweenProgressNum) self.comProgress.progress += 0.01;
                }
            }).repeatForever();
        this.tweenProgress.start();


        // SdkMgr.ins.hideWxCustomAd('pre1');
        // SdkMgr.ins.hideWxCustomAd('pre2');
        this._continue = true;
        this.preLayer.active = false;
        this.enterGame();
    }
    onEnable() {
    }
    onDisable() {
        if (this.comProgress) this.comProgress.progress = 1;
        if (this.comProgress2) this.comProgress2.progress = 1;
        if (this.tweenProgress) this.tweenProgress.stop();
        if (this.tweenProgress2) this.tweenProgress2.stop();
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
}
